import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;

/**
 * 问题描述：足球联赛采用主客场双循环赛制，胜一场得3分，平局各得1分，负一场得0分，联赛排名以积分多者在前，
 * 当两队（或多队）积分相同时，则净胜球（即进球数与失球数之差）多者在前，
 * 若净胜球相同，则进球数多者在前，若仍相同，则抽签或踢附加赛决定名次（这在联赛结束后进行，联赛未结束则两队名次并列，本程序不做这方面要求）。
 * 试编一程序统计最近一轮比赛后，各队积分及排名。
 * 基本要求：设积分表结构如下：队名（不超过15个字符），已比赛的场数，赢的场数，平的场数，负的场数，进球数，失球数，积分。
 * 积分表放在正文文件中。最近一轮的结果从键盘输入，
 * 其形式为：主队名（可用代码），客队名（可用代码），主队得分（即进球数），客队得分（即进球数）。
 * 程序应根据此轮结果修改各队的积分和名次，所得的最新记分表仍在原积分文件中并同时在屏幕上显示。
 * 测试数据：可选择我国当年的甲A或甲B联赛的数据输入，并检查与报章公布的数据是否一致。
 * 实现提示：定义一个球队类，每个球队是均是此类的对象。
 * 由于联赛中参赛的队伍数是固定的，因此可用对象数组来实现（当然也可以用链表结构）。
 * 每输入两个队的比赛成绩，则相应的队的有关数据（比赛场数，赢的场数，平的场数，负的场数，进球数，失球数，积分等）即可进行修改，
 * 比赛成绩录入完成，调用联赛排序方法（对象数组作为参数）排出名次并输出
 */

public class 实验五足球联赛积分 {

    public static void main(String[] args) throws FileNotFoundException {
        Scanner fileInput = new Scanner(new File("./实验五足球联赛积分测试数据.txt"));
        Scanner sc = new Scanner(System.in);
        String str = fileInput.nextLine();

        Map<String, FootballTeam> map = new HashMap<>();
        ArrayList<FootballTeam> list = new ArrayList<>();

        while (fileInput.hasNext()) {
            String s = fileInput.nextLine();

            String[] split = s.split(",");

            String name = split[0];
            int count = Integer.parseInt(split[1]);
            int winCount = Integer.parseInt(split[2]);
            int drawCount = Integer.parseInt(split[3]);
            int failCount = Integer.parseInt(split[4]);
            int winNumber = Integer.parseInt(split[5]);
            int failNumber = Integer.parseInt(split[6]);
            int integral = Integer.parseInt(split[7]);
            
            FootballTeam footballTeam = new FootballTeam(name,count,winCount,drawCount,failCount,winNumber,failNumber,integral);
            map.put(name,footballTeam);
            list.add(footballTeam);
        }

        // 输入 对局信息
        System.out.println("请输入对局场数");
        int matchCount = sc.nextInt();
        for (int i = 0; i < matchCount; i++) {
            System.out.println("请输入第" + (i + 1) + "场比赛的信息（主队名, 客队名, 主队得分, 客队得分）:");
            String homeTeamName = sc.next();
            String awayTeamName = sc.next();
            int homeScore = sc.nextInt();
            int awayScore = sc.nextInt();

            FootballTeam homeTeam = map.get(homeTeamName);
            FootballTeam awayTeam = map.get(awayTeamName);

            homeTeam.update(homeScore, awayScore);
            awayTeam.update(awayScore, homeScore);
        }

        list.sort(new Comparator<FootballTeam>() {
            @Override
            public int compare(FootballTeam o1, FootballTeam o2) {
                if (o1.getIntegral() != o2.getIntegral()) { // 以积分多者在前
                    return o2.getIntegral() - o1.getIntegral();
                } else {
                    int res1 = o1.getWinNumber() - o1.getFailNumber();
                    int res2 = o2.getWinNumber() - o2.getFailNumber();

                    if (res1 != res2) {  // 净胜球（即进球数与失球数之差）多者在前
                        return res2 - res1;
                    } else {
                        int winNumber1 = o1.getWinNumber();
                        int winNumber2 = o2.getWinNumber();

                        if (winNumber1 != winNumber2) { // 进球数多者在前
                            return winNumber2 - winNumber1;
                        } else {
                            return 0;
                        }
                    }
                }
            }
        });

        System.out.println(str);

        for(FootballTeam footballTeam : list){
            System.out.println(footballTeam);
        }
    }

    public static class FootballTeam {

        /**
         * 队伍名字
         */
        private String teamName;

        /**
         * 已比赛的场数
         */
        private int count;

        /**
         * 赢的场数
         */
        private int winCount;

        /**
         * 平的场数
         */
        private int drawCount;

        /**
         * 负的场数
         */
        private int failCount;

        /**
         * 进球数
         */
        private int winNumber;

        /**
         * 失球数
         */
        private int failNumber;

        /**
         * 积分
         */
        private int integral;

        public FootballTeam() {

        }

        public FootballTeam(String teamName, int count, int winCount, int drawCount, int failCount, int winNumber, int failNumber, int integral) {
            this.teamName = teamName;
            this.count = count;
            this.winCount = winCount;
            this.drawCount = drawCount;
            this.failCount = failCount;
            this.winNumber = winNumber;
            this.failNumber = failNumber;
            this.integral = integral;
        }


        public void update(int score, int opponentScore) {
            count++;
            if (score > opponentScore) {
                winCount++;
                integral += 3;
            } else if (score == opponentScore) {
                drawCount++;
                integral += 1;
            } else {
                failCount++;
            }
            winNumber += score;
            failNumber += opponentScore;
        }

        public String getTeamName() {
            return teamName;
        }

        public void setTeamName(String teamName) {
            this.teamName = teamName;
        }

        public int getCount() {
            return count;
        }

        public void setCount(int count) {
            this.count = count;
        }

        public int getWinCount() {
            return winCount;
        }

        public void setWinCount(int winCount) {
            this.winCount = winCount;
        }

        public int getDrawCount() {
            return drawCount;
        }

        public void setDrawCount(int drawCount) {
            this.drawCount = drawCount;
        }

        public int getFailCount() {
            return failCount;
        }

        public void setFailCount(int failCount) {
            this.failCount = failCount;
        }

        public int getWinNumber() {
            return winNumber;
        }

        public void setWinNumber(int winNumber) {
            this.winNumber = winNumber;
        }

        public int getFailNumber() {
            return failNumber;
        }

        public void setFailNumber(int failNumber) {
            this.failNumber = failNumber;
        }

        public int getIntegral() {
            return integral;
        }

        public void setIntegral(int integral) {
            this.integral = integral;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            FootballTeam that = (FootballTeam) o;
            return count == that.count && winCount == that.winCount && drawCount == that.drawCount && failCount == that.failCount && winNumber == that.winNumber && failNumber == that.failNumber && integral == that.integral && Objects.equals(teamName, that.teamName);
        }

        @Override
        public int hashCode() {
            return Objects.hash(teamName, count, winCount, drawCount, failCount, winNumber, failNumber, integral);
        }

        @Override
        public String toString() {
            return "FootballTeam{" +
                    "teamName='" + teamName + '\'' +
                    ", count=" + count +
                    ", winCount=" + winCount +
                    ", drawCount=" + drawCount +
                    ", failCount=" + failCount +
                    ", winNumber=" + winNumber +
                    ", failNumber=" + failNumber +
                    ", integral=" + integral +
                    '}';
        }
    }
}
